package com.leetcode.剑指offer专项突击版;

/**
 * @author 覃国强
 * @date 2022-02-28 20:08
 */
public class 剑指OfferII_008_和大于等于target的最短子数组 {

  public static void main(String[] args) {
    int target = 7;
    int[] nums = {2,3,1,2,4,3 };
    Solution solution = new Solution();
    int len = solution.minSubArrayLen(target, nums);
    System.out.println(len);
  }

  static
  class Solution {
    public int minSubArrayLen(int target, int[] nums) {
      if (nums == null || nums.length == 0) {
        return 0;
      }

      int left = 0;
      int right = 0;
      int sum = 0;
      int answer = Integer.MAX_VALUE;

      for (; right < nums.length; ++right) {

        sum += nums[right];
        while (sum >= target) {
          answer = Math.min(right - left + 1, answer);
          sum -= nums[left];
          ++left;
        }

      }

      return answer == Integer.MAX_VALUE ? 0 : answer;
    }
  }

}
